﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MaterialEditor
{
    public class InvertNode : Node
    {
        InputSlot A
        {
            get { return input[0]; }
        }

        NodeValue result;

        private NodeValueType[][] _acceptTypes;
        public override NodeValueType[][] acceptTypes
        {
            get { return _acceptTypes; }
        }

        public InvertNode(int x, int y)
            :base(x, y)
        {
            input = new InputSlot[1]
            {
                new InputSlot(this, null, "A"),
            };

            _acceptTypes = new NodeValueType[][]
            {
                new NodeValueType[] { NodeValueType.ValueTexture },
            };

            output = new InputSlot(this, null, "");

            A.Connected += A_Connected;
            A.ValueChanged += A_Connected;
            A.Disconnected += A_Disconnected;

            Name = "Invert";
        }

        void A_Disconnected()
        {
            result = null;
        }

        void A_Connected()
        {
            if (A.value.Value.GetType() != typeof(TextureContainer))
            {
                A.connector.Disconnect();
                result = null;
                return;
            }

            TextureContainer newVal = new TextureContainer(A.value.AsTexture2D());
            Invert i = new Invert();

            newVal.texture.effects += (A.value.Value as TextureContainer).texture.effects;
            newVal.texture.effects += i;

            result = new NodeValue(newVal);
        }

        public override NodeValue Result
        {
            get { return result; }
        }

        public override object Clone()
        {
            InvertNode n = new InvertNode((int)position.X, (int)position.Y);
            if (result != null)
                n.result = new NodeValue(result.Value);

            return n;
        }
    }
}